<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../dbcom/RagionCom.php");
require_once(dirname(__FILE__) . "/../common/Common.php");

$common = new Common();
$ragioncom = new RagionCom();

$cont = $_REQUEST['cont'];
switch ($cont){
    case 1:
        $ragionid = $_REQUEST['ragionid'];
        if(empty($ragionid)){
            echo json_encode(array("code" => 100, "msg" => "empty ragionid"));
            break;
        }
        $res = $ragioncom->deleteRagion($ragionid);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success","data"=>$ragionid));
        }else{
            echo json_encode(array("code" => 400, "msg" => "delete error"));
        }
        break;
    case 2:
        $name = $_REQUEST['name'];
        if(empty($name)){
            echo json_encode(array("code" => 100, "msg" => "empty name"));
            break;
        }
        $res = $ragioncom->addRagion($name, 0);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success"));
        }else{
            echo json_encode(array("code" => 400, "msg" => "add error"));
        }
        break;
    case 3:
        $name = $_REQUEST['name'];
        $parentid = $_REQUEST['parent'];
        if(empty($name)){
            echo json_encode(array("code" => 100, "msg" => "empty name"));
            break;
        }
        $res = $ragioncom->addRagion($name, 1, $parentid);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success"));
        }else{
            echo json_encode(array("code" => 400, "msg" => "add error"));
        }
        break;
    case 4:
        $name = $_REQUEST['name'];
        $parentid = $_REQUEST['parent'];
        if(empty($name)){
            echo json_encode(array("code" => 100, "msg" => "empty name"));
            break;
        }
        $res = $ragioncom->addRagion($name, 2, $parentid);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success"));
        }else{
            echo json_encode(array("code" => 400, "msg" => "add error"));
        }
        break;
    case 5:
        $name = $_REQUEST['name'];
        $ragionid = $_REQUEST['ragionid'];
        if(empty($name) || empty($ragionid)){
            echo json_encode(array("code" => 100, "msg" => "empty name"));
            break;
        }
        $res = $ragioncom->updateRagion($name, 0, 0, $ragionid);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success"));
        }else{
            echo json_encode(array("code" => 400, "msg" => "change error"));
        }
        break;
    case 6:
        $name = $_REQUEST['name'];
        $ragionid = $_REQUEST['ragionid'];
        $parentid = $_REQUEST['parentid'];
        if(empty($name) || empty($ragionid) || empty($parentid)){
            echo json_encode(array("code" => 100, "msg" => "empty name"));
            break;
        }
        $res = $ragioncom->updateRagion($name, 1, $parentid, $ragionid);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success"));
        }else{
            echo json_encode(array("code" => 400, "msg" => "change error"));
        }
        break;
    case 7:
        $name = $_REQUEST['name'];
        $ragionid = $_REQUEST['ragionid'];
        $parentid = $_REQUEST['parentid'];
        if(empty($name) || empty($ragionid) || empty($parentid)){
            echo json_encode(array("code" => 100, "msg" => "empty name"));
            break;
        }
        $res = $ragioncom->updateRagion($name, 2, $parentid, $ragionid);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success"));
        }else{
            echo json_encode(array("code" => 400, "msg" => "change error"));
        }
        break;
    case 10:
        $ragionid = $_REQUEST['ragionid'];
        $ragioninfo = $ragioncom->getRagionById($ragionid);
        if(!$ragioninfo){
            echo json_encode(array("code" => 400, "msg" => "not find"));
        }else{
            echo json_encode(array("code" => 0, "msg" => "success","data"=>array("name"=>$ragioninfo['name'],"parent"=>$ragioninfo['parent_id'])));
        }
        break;
    default :
        echo json_encode(array("code" => 500, "msg" => "undefined error"));
        break;
}
exit();
